//
//  WMH_SettingView.m
//  iphone.mobileoa
//
//  Created by 李林 on 13-10-25.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "WMH_Setting_ViewController.h"

#define SetCheckUpdateTaskId 2

@interface WMH_Setting_ViewController ()

@end

@implementation WMH_Setting_ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

-(void)viewDidAppear:(BOOL)animated{
    if([[DBFrameService getConfigValue:WMH_IsLogin]isEqual:@"1"]){
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"注销" style:UIBarButtonItemStylePlain target:self action:@selector(logout:)];
        self.navigationItem.rightBarButtonItem = rightItem;
        [mTableView reloadData];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"系统设置";
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    mTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480 - 20 - 44 - 30) style:UITableViewStyleGrouped];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    
    //UITableViewStyleGrouped时,要这么设置背景,否则无效
    mTableView.backgroundView = nil;
    mTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:mTableView];
 
    itemArrays = [[NSMutableArray alloc]initWithObjects:@"我的微门户",@"字体设置",@"清理缓存",@"当前版本",@"关于我们",nil];
    
    subItemArrays = [[NSMutableArray alloc]initWithObjects:@"", nil];
    

    
    
    
}



#pragma mark tableview

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [itemArrays count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"_ContentCELL";
    TDBadgedCell *cell = [[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    //登录状态
    if (indexPath.row==0)
    {
        if ([[DBFrameService getConfigValue:WMH_IsLogin] isEqualToString:@"1"]) {
            cell.badgeString = @"已登录";
        }else{
            cell.badgeString = @"未登录";
        }
        cell.badgeColor = [UIColor colorWithRed:0.197 green:0.592 blue:0.219 alpha:1.000];
        cell.badge.radius = 9;
    }
    
    //字体设置
    if (indexPath.row==1) {
        NSLog(@"%@",[DBFrameService getConfigValue:WMH_NewsContent_FontSize]);
        if([[DBFrameService getConfigValue:WMH_NewsContent_FontSize]isEqualToString:wmh_font_big]){
            cell.badgeString = @"大字体";
        }else if([[DBFrameService getConfigValue:WMH_NewsContent_FontSize] isEqualToString:wmh_font_middle]){
            cell.badgeString = @"中字体";
        }else if([[DBFrameService getConfigValue:WMH_NewsContent_FontSize]isEqualToString:wmh_font_small]){
            cell.badgeString = @"小字体";
        }
        
        cell.badgeColor = [UIColor colorWithRed:0.197 green:0.592 blue:0.219 alpha:1.000];
        cell.badge.radius = 9;
    }
    
    //当前版本
    if (indexPath.row==3)
    {
        NSDictionary *dic = [[NSBundle mainBundle]infoDictionary];
        NSString *version = [dic objectForKey:@"CFBundleShortVersionString"];
        cell.badgeString = version;
        cell.badgeColor = [UIColor colorWithRed:0.197 green:0.592 blue:0.219 alpha:1.000];
        cell.badge.radius = 9;
    }
    
    //设置cell 样式
    cell.selectionStyle = UITableViewStyleGrouped;
    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    
    cell.textLabel.font = [UIFont fontWithName:wmh_listitem_font size:15.0];
    cell.textLabel.text = [itemArrays objectAtIndex:indexPath.row];
    
    return cell;
}


- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //设置字体
    if (indexPath.row == 1) {
        [self setAppFont];
    }
    //清除缓存
    else if (indexPath.row == 2){
        [self clearAppCache];
    }
    //关于我们
    else if(indexPath.row == 4){
        WMH_AboutUsView *view = [[WMH_AboutUsView alloc]init];
        [self.navigationController pushViewController:view animated:YES];
    }
    //版本更新
    else if(indexPath.row == 3){
//        [self startTask:@"EpointCheckUpdateTask" withParams:nil withTaskId:SetCheckUpdateTaskId];
    }
    //我的微门户
    else if(indexPath.row == 0){
        if([[DBFrameService getConfigValue:WMH_IsLogin]isEqual:@"1"]){
            WMH_User_Detail_ViewController *view = [[WMH_User_Detail_ViewController alloc]init];
            [self.navigationController pushViewController:view animated:YES];
        }else{
            WMH_Login_ViewController *view = [[WMH_Login_ViewController alloc]init];
            [self.navigationController pushViewController:view animated:YES];
        }
    }

    
}


#pragma mark 设置字体
-(void)setAppFont{
    RIButtonItem *bigItem = [RIButtonItem item];
    bigItem.label = @"大号字";
    bigItem.action = ^
    {
        [DBFrameService setConfigValue:WMH_NewsContent_FontSize value:wmh_font_big];//大22，中19，小16
        [mTableView reloadData];
    };
    
    RIButtonItem *middleItem = [RIButtonItem item];
    middleItem.label = @"中号字";
    middleItem.action = ^
    {
       [DBFrameService setConfigValue:WMH_NewsContent_FontSize value:wmh_font_middle];
        [mTableView reloadData];
    };
    
    RIButtonItem *smallItem = [RIButtonItem item];
    smallItem.label = @"小号字";
    smallItem.action = ^
    {
       [DBFrameService setConfigValue:WMH_NewsContent_FontSize value:wmh_font_small];
        [mTableView reloadData];
    };
    RIButtonItem *cancelItem = [RIButtonItem item];
    cancelItem.label = @"取消";
    cancelItem.action = ^
    {
        
    };
    
    
    UIAlertView *alter = [[UIAlertView alloc]
                          initWithTitle:@"字体设置"
                          message:@""
                          cancelButtonItem:cancelItem
                          otherButtonItems:bigItem,middleItem,smallItem,nil];
    [alter show];
}

#pragma mark 清空缓存
-(void)clearAppCache{
    RIButtonItem *okItem = [RIButtonItem item];
    okItem.label = @"确定";
    okItem.action = ^
    {
        //清空缓存图片
        NSArray *documentspath=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
        NSString *docDir=[documentspath objectAtIndex:0];
        
        NSString *savePath=[docDir stringByAppendingPathComponent:wmh_cache_image];
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        if(![fileManager fileExistsAtPath:[docDir stringByAppendingPathComponent:wmh_cache_image]])
        {
            [fileManager createDirectoryAtPath:savePath withIntermediateDirectories:YES attributes:nil error:nil];
        }
        NSString *extension = @"jpg";
        NSArray *contents = [fileManager contentsOfDirectoryAtPath:savePath error:NULL];
        NSEnumerator *e = [contents objectEnumerator];
        NSString *filename;
        while ((filename = [e nextObject])) {
            NSString * fileExtentsion =[filename pathExtension];
            if ([fileExtentsion isEqualToString:extension]) {
                
                [fileManager removeItemAtPath:[savePath stringByAppendingPathComponent:filename] error:NULL];
            }
        }
        
        //清空附件
        //            NSString *attachpath = [NSString stringWithFormat:@"%@/Documents/attaches/",NSHomeDirectory()];
        //            [fileManager removeItemAtPath:attachpath error:nil];
        
        
        //清空数据库缓存
        [DBFrameService deleteFrameCache];
        
        [self toastMessage:@"清空完成"];
        
    };
    
    RIButtonItem *noItem = [RIButtonItem item];
    noItem.label = @"取消";
    
    //调用
    NSString *mess = [NSString stringWithFormat:@"确定要清理缓存吗？"];
    
    UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:mess cancelButtonItem:noItem otherButtonItems:okItem, nil];
    [alter show];
}
#pragma mark 注销
-(void)logout:(id)sender
{
    RIButtonItem *okItem = [RIButtonItem item];
    okItem.label = @"确定";
    okItem.action = ^
    {
        
        [DBFrameService setConfigValue:WMH_IsLogin value:@"0"];
        self.navigationItem.rightBarButtonItem = nil;
        [mTableView reloadData];
    };
    
    RIButtonItem *noItem = [RIButtonItem item];
    noItem.label = @"取消";
    
    //调用
    NSString *mess = [NSString stringWithFormat:@"确定要注销登录吗？"];
    
    UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:mess cancelButtonItem:noItem otherButtonItems:okItem, nil];
    [alter show];
}


#pragma mark 刷新页面
-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj{
    if (taskId==SetCheckUpdateTaskId) {
        [EpointUpdateService validateUpdateInfo:obj vc:self flag:1];
    }
}



@end
